package de.tum.wi.som.prio.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import de.tum.wi.som.Node;
import de.tum.wi.som.Project;
import de.tum.wi.som.prio.PriorityRule;
import de.tum.wi.som.util.PrioUtil;

/**
 * Random
 * 
 * @author Goetz Epperlein
 */
public class RAN implements PriorityRule {

	@Override
	public void prioritize(Project project) {
		
		Node source = project.getSource();

		ArrayList<Node> nodesBefor = new ArrayList<Node>();
		ArrayList<Node> nodesAfter = new ArrayList<Node>();
		List<Node> accessibleNodes;
		Random rand = new Random();

		nodesBefor.addAll(Arrays.asList(project.getNodes()));
		
		// copy nodes randomly to new array
		while (!nodesBefor.isEmpty()) {
			accessibleNodes = PrioUtil.determineAccesibleNodes(nodesAfter, source, nodesBefor);
            if (accessibleNodes.size() == 0) {
            	System.err.println("No accessible nodes found in Random");
            	return;
            }
            Node candidateNode = accessibleNodes.get(rand.nextInt(accessibleNodes.size()));
            nodesBefor.remove(candidateNode);
			nodesAfter.add(candidateNode);
		}

		// set new random node list as given
		project.setNodes(nodesAfter.toArray(new Node[nodesAfter.size()]));
	}
	
	@Override
	public String toString(){
		return "Random";
	}
}
